<?php
namespace Framework\Library\Validator;

use Phalcon\Validation;
use Phalcon\Validation\Validator;
use Phalcon\Validation\ValidatorInterface;
use Phalcon\Validation\Message;

class BinaryValidator extends Validator implements ValidatorInterface
{
    public function validate(Validation $validation, $attribute)
    {
        if (!isset($_FILES[$attribute]) || $_FILES[$attribute]['error'] != UPLOAD_ERR_OK) {
            $validation->appendMessage(new Message('File upload error', $attribute, 'Binary'));
        }

        $binaryFile = $_FILES[$attribute];
        if (!empty($binaryFile['size']) && $binaryFile['size'] <= (int) $this->getOption('maxsize')) {
            return true;
        }

        $validation->appendMessage(new Message('File size is too large', $attribute, 'Binary'));

        return false;
    }
}
